<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <SearchBar :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"
          :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          @row-click="edit"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <!--          <el-table-column type="selection" align="center" />-->
          <el-table-column label="状态" min-width="120">
            <template slot-scope="scope">
              <div v-if="scope.row.status=='0'" class="status_space pending_payment">
                <svg-icon icon-class="待收款" />
                <span>待付款</span>
              </div>
              <div v-if="scope.row.status=='1'" class="status_space insufficient_space2">
                <svg-icon icon-class="不足" />
                <span>审批中</span>
              </div>
              <div v-if="scope.row.status=='2'" class="status_space insufficient_space">
                <svg-icon icon-class="驳回" />
                <span>已驳回</span>
              </div>
              <div v-if="scope.row.status=='3'" class="status_space adequate_space">
                <svg-icon icon-class="通过" />
                <span>已通过</span>
              </div>
              <div v-if="scope.row.status=='4'" class="status_space under_execution">
                <svg-icon icon-class="执行中" />
                <span>部分付款</span>
              </div>
              <div v-if="scope.row.status=='5'" class="status_space adequate_space">
                <svg-icon icon-class="通过" />
                <span>全部付款</span>
              </div>
            </template>
          </el-table-column>
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" />
          <el-table-column
            label="附件"
            min-width="80"
          >
            <template slot-scope="scope">
              <FileManage
                :file-names.sync="scope.row.fileName"
                :file-real-names.sync="scope.row.fileRealName"
                :file-urls.sync="scope.row.fileUrl"
                :row-index="scope.$index"
                :readonly="true"
                class="accessory"
                @fileName="setFileName"
              />
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>

  </div>
</template>

<script>
import { list, getInfo } from '@/api/receivableAndPayable/receivableAndPayableManage/paymentOrder'
import AddOrUpdate from './addOrUpdate'
import ShowMaterialDetail from './showMaterialDetail'
import { mapGetters } from 'vuex'
export default {
  name: 'Index',
  data() {
    return {
      loading: true,
      pageId: '付款单', // 页面唯一标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc'
      },
      // tableKey: 1,
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      isEdit: true,
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'code', label: '付款单号' },
        { prop: 'receiptByName', label: '制单人员', canOrder: false },
        { prop: 'receiptTime', label: '制单日期' },
        { prop: 'supplierName', label: '供应商', canOrder: false },
        { prop: 'formCode', label: '关联订单' },
        { prop: 'taxAllPrice', label: '含税总金额' },
        { prop: 'hasReceiptPrice', label: '已开票金额' },
        { prop: 'unReceiptPrice', label: '未开票金额' },
        { prop: 'hasPrice', label: '已付款金额' },
        { prop: 'unPrice', label: '未付款金额' },
        { prop: 'remark', label: '备注', canOrder: false }

      ],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'code', label: '付款单号', type: 'input' },
        { prop: 'receiptByName', label: '制单人员', type: 'input' },
        { prop: 'receiptTime', label: '制单日期', type: 'time' },
        { prop: 'supplierName', label: '供应商', type: 'input' },
        { prop: 'formCode', label: '关联订单', type: 'input' },
        { prop: 'statusList', label: '状态', type: 'select-multiple', optionsName: 'statusOptions' }
      ],
      statusOptions: [ // 选择组件数据源
        { value: '0', label: '待付款' },
        { value: '1', label: '审批中' },
        { value: '2', label: '已驳回' },
        { value: '3', label: '已通过' },
        { value: '4', label: '部分付款' },
        { value: '5', label: '全部付款' }
      ],
      showKey: [
        {
          name: '基本信息',
          columns: [
            { type: 'form', name: '付款单号', key: 'code' },
            { type: 'form', name: '制单人员', key: 'receiptByName' },
            { type: 'form', name: '制单日期', key: 'receiptTime' },
            { type: 'form', name: '供应商', key: 'supplierName' },
            { type: 'form', name: '关联订单', key: 'formCode' },
            { type: 'form', name: '含税总金额', key: 'taxAllPrice' },
            { type: 'form', name: '已开票金额', key: 'hasReceiptPrice' },
            { type: 'form', name: '未开票金额', key: 'unReceiptPrice' },
            { type: 'form', name: '已付款金额', key: 'hasPrice' },
            { type: 'form', name: '未付款金额', key: 'unPrice' },
            { type: 'file1', name: '订单附件', orderFileName: 'orderFileName', orderFileRealName: 'orderFileRealName', orderUrl: 'orderUrl' },
            { type: 'file', name: '附件', fileName: 'fileName', fileRealName: 'fileRealName', fileUrl: 'fileUrl' },
            { type: 'form', name: '备注', key: 'remark', length: 'double' },
            {
              type: 'table', name: '', key: 'details'
            }
          ]
        },
        {
          name: '供应商收款信息',
          columns: [
            {
              type: 'table2', name: '', key: 'concatSupplierList', columns: [ // type: 'table' 表示是表格
                { name: '银行账号', key: 'bankAccount' },
                { name: '账户名称', key: 'accountName' },
                { name: '收款银行', key: 'dueBank' },
                { name: '注意事项', key: 'announcements' }
              ]
            }
          ]
        },
        {
          name: '审批详情',
          columns: [
            { type: 'review', name: '评审进度', key: 'taskList' }
          ]
        }
      ],
      operationData: [ // 右上角的操作按钮
        { label: '页面配置', perm: '' }
      ]
    }
  },
  created() {
    const newData = this.$store.state.user.permissions // 看有没有编辑权限
    this.isEdit = newData.includes('finance:out:edit')
  },
  computed: {
    ...mapGetters([
      'notificationId', 'userName'
    ])
  },
  watch: {
    notificationId(newValue, oldValue) {
      if (newValue !== oldValue && newValue !== 0 && this.$store.state.notification.notificationStatus == '1') {
        const rowId = { id: this.notificationId }
        this.showDetail(rowId)
      }
    }
  },
  mounted() {
    if (this.notificationId !== 0) {
      const rowId = { id: this.notificationId }
      if (this.$store.state.notification.notificationStatus == '1') {
        this.showDetail(rowId)
      }
    }
  },
  methods: {
    getExportData(data) {
      return list(data)
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    refresh2() {
      this.tableData = []
      setTimeout(() => {
        this.getList()
      }, 50)
    },
    showDetail(row) {
      getInfo(row.id).then(res => {
        if (res.data.status != 0 && res.data.isApprovalShow != 1) {
          const obj = {
            taskNoName: '审批结束',
            taskList: []
          }
          if (res.data.taskList[res.data.taskList.length - 1].approvalStatus == 3 || res.data.taskList[res.data.taskList.length - 1].approvalStatus == 4) {
            obj.approvalStatus = 5
          } else {
            obj.approvalStatus = 0
          }
          res.data.taskList.push(obj)
        } // 跳转过来直接查看审批
        this.$layer.iframe({// 弹框
          content: {
            content: ShowMaterialDetail, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              showData: res.data,
              businessId: row[this.mainId],
              showKey: this.showKey,
              isSaleOrder: true
            }// props
          },
          area: ['1320px', '700px'],
          title: '查看付款单', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      })
      this.$store.dispatch('notification/changeId', 0)
    },
    getList() {
      list(this.pageData).then(res => {
        this.tableData = res.data
        this.total = res.total
        this.loading = false
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    edit(row) {
      if (this.isEdit) {
        if (row.status == '1') {
          getInfo(row.id).then(res => {
            console.log(res.data, 1111111111111111111)
            if (res.data.status != 0 && res.data.isApprovalShow != 1) {
              const obj = {
                taskNoName: '审批结束',
                taskList: []
              }
              if (res.data.taskList[res.data.taskList.length - 1].approvalStatus == 3 || res.data.taskList[res.data.taskList.length - 1].approvalStatus == 4) {
                obj.approvalStatus = 5
              } else {
                obj.approvalStatus = 0
              }
              res.data.taskList.push(obj)
            } // 跳转过来直接查看审批
            this.$layer.iframe({// 弹框
              content: {
                content: ShowMaterialDetail, // 传递的组件对象
                parent: this, // 当前的vue对象
                data: {
                  showData: res.data,
                  businessId: row[this.mainId],
                  showKey: this.showKey,
                  isSaleOrder: true
                }// props
              },
              area: ['1320px', '700px'],
              title: '查看付款单', // 标题
              shadeClose: false // 点击遮罩是否关闭
            })
          })
        } else {
          this.$layer.iframe({// 弹框
            content: {
              content: AddOrUpdate, // 传递的组件对象
              parent: this, // 当前的vue对象
              data: {
                id: row[this.mainId]
              }// props
            },
            area: ['1320px', '700px'],
            title: '记录付款单', // 标题
            shadeClose: false // 点击遮罩是否关闭
          })
        }
      } else {
        getInfo(row.id).then(res => {
          if (res.data.status != 0 && res.data.isApprovalShow != 1) {
            const obj = {
              taskNoName: '审批结束',
              taskList: []
            }
            if (res.data.taskList[res.data.taskList.length - 1].approvalStatus == 3 || res.data.taskList[res.data.taskList.length - 1].approvalStatus == 4) {
              obj.approvalStatus = 5
            } else {
              obj.approvalStatus = 0
            }
            res.data.taskList.push(obj)
          } // 跳转过来直接查看审批
          this.$layer.iframe({// 弹框
            content: {
              content: ShowMaterialDetail, // 传递的组件对象
              parent: this, // 当前的vue对象
              data: {
                showData: res.data,
                businessId: row[this.mainId],
                showKey: this.showKey,
                isSaleOrder: true
              }// props
            },
            area: ['1320px', '700px'],
            title: '查看付款单', // 标题
            shadeClose: false // 点击遮罩是否关闭
          })
        })
      }
    },
    setFileName(...data) {
      this.tableData[data[3]].fileName = data[0]
      this.tableData[data[3]].fileRealName = data[1]
      this.tableData[data[3]].fileUrl = data[2]
    }
  }
}
</script>

<style lang="less" scoped>
.pending_payment {
  background-color: #D1EAFF;
  color: #73c0ff;
  border-radius: 3px;
  padding: 3px 0;
  width: 80px;
  text-align: center;
}
.under_execution {
  color: rgb(17, 109, 252);
  background-color: rgb(230, 243, 255);
  border-radius: 3px;
  padding: 3px 0;
  width: 80px;
  text-align: center;
}
.adequate_space {
  color: #19be6b;
  background-color: rgb(223, 249, 241);
  border-radius: 3px;
  padding: 3px 0;
  width: 80px;
  text-align: center;
}
.insufficient_space {
  color: rgb(231, 108, 108);
  background-color: rgb(255, 238, 238);
  border-radius: 3px;
  padding: 3px 0;
  width: 80px;
  text-align: center;
}
.insufficient_space2 {
  color: rgb(255, 151, 32);
  background-color: rgb(255, 245, 239);
  border-radius: 3px;
  padding: 3px 0;
  width: 80px;
  text-align: center;
}
.revoke_space {
  color: #999999;
  background-color: rgb(242, 242, 242);
  border-radius: 3px;
  padding: 3px 0;
  width: 80px;
  text-align: center;
}
.status_space{
  width: 100%;
  span{
    padding: 0 5px;
  }
}
</style>
